package admin

import (
	_ "embed"
	. "go-caipu/pkg/services/sqlstore/migrator"
)

//go:embed sql/sys_api.sql
var ApiSQLByte []byte

//go:embed sql/sys_api_category.sql
var categorySQLByte []byte

func AddApiMigrations(mg *Migrator) {
	apiV1 := Table{
		Name: "sys_api",
		Columns: []*Column{
			{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: false},
			{Name: "category", Type: DB_BigInt, Nullable: true},
			{Name: "handle", Type: DB_NVarchar, Length: 128, Nullable: true},
			{Name: "title", Type: DB_NVarchar, Length: 128, Nullable: false},
			{Name: "path", Type: DB_NVarchar, Length: 128, Nullable: true},
			{Name: "type", Type: DB_NVarchar, Length: 16, Nullable: true},
			{Name: "action", Type: DB_NVarchar, Length: 64, Nullable: true},
			{Name: "remark", Type: DB_NVarchar, Length: 256, Nullable: true},

			{Name: "create_by", Type: DB_BigInt, Nullable: false},
			{Name: "update_by", Type: DB_BigInt, Nullable: true},
			{Name: "created_at", Type: DB_DateTime, Nullable: false},
			{Name: "updated_at", Type: DB_DateTime, Nullable: true},
		},
		Indices: []*Index{
			{Cols: []string{"category"}, Type: IndexType},
		},
	}
	categoryV1 := Table{
		Name: "sys_api_category",
		Columns: []*Column{
			{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: false},
			{Name: "name", Type: DB_NVarchar, Length: 128, Nullable: true},
			{Name: "parent_id", Type: DB_BigInt, Length: 128, Nullable: false},

			{Name: "create_by", Type: DB_BigInt, Nullable: false},
			{Name: "update_by", Type: DB_BigInt, Nullable: true},
			{Name: "created_at", Type: DB_DateTime, Nullable: false},
			{Name: "updated_at", Type: DB_DateTime, Nullable: true},
		},
	}

	mg.AddMigration("create sys_api table", NewAddTableMigration(apiV1))

	mg.AddMigration("create sys_api category index", NewAddIndexMigration(apiV1, apiV1.Indices[0]))
	mg.AddMigration("insert into sys_api", NewRawSQLMigration(string(ApiSQLByte)))

	mg.AddMigration("create sys_api_category table", NewAddTableMigration(categoryV1))
	mg.AddMigration("insert into sys_api_category", NewRawSQLMigration(string(categorySQLByte)))
}
